package com.dmcb.trade.business.services;

import com.dmcb.common.business.services.BaseService;
import com.dmcb.common.business.utils.*;
import com.dmcb.common.web.conversion.JsonResult;
import com.dmcb.common.business.utils.WebClientUtil;
import com.dmcb.trade.business.constants.CrawlConstant;
import com.dmcb.trade.business.crawlers.article.BaseParser;
import com.dmcb.trade.business.entities.MonitorWechat;
import com.dmcb.trade.business.mappers.ArticleMonitorMapper;
import com.dmcb.trade.business.utils.CrawlerUtil;
import com.dmcb.trade.controller.ArticleCrawlerController;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;


/**
 * Created by 63221_000 on 2016/8/3 0003.
 */
@Service
public class WeChatService extends BaseService {
    Logger logger = LoggerFactory.getLogger(WeChatService.class);
    @Autowired
    private ArticleCrawlerController articleCrawlerController;

    @Autowired
    private ArticleMonitorMapper articleMonitorMapper;
    @Autowired
    ArticleCrawlerService articleCrawlerService;
    @Autowired
    private WeChatCheckService weChatCheckService;

    @Autowired
    private WebClientUtil webClientUtil;

    @Autowired
    private RedisUtils redisUtils;


    public JsonResult crawl(String link, Integer weChatId, String cookie, int type) throws Exception {
        int page = 1;
        int ero = 0;
        int success = 0;
        String src;
        if (StringUtils.isBlank(link) || weChatId == null || StringUtils.isBlank(cookie)) {
            return error("文章链接或微信检测ID或Cookie，尚未填写");
        }
        if (!cookie.contains("XIGUASTATE")) {
            return error("西瓜助手尚未登陆");
        }
        MonitorWechat monitorWechat = articleMonitorMapper.queryById(weChatId);
        if (monitorWechat == null) {
            monitorWechat = new MonitorWechat();
            articleMonitorMapper.insertWechat(monitorWechat);
        }
        link = link.substring(0, link.lastIndexOf("/") + 1);
        WebClientUtil.wc2 = webClientUtil.initXiGuaZhuShou(cookie);
        Document doc;
        Elements elements;
        JsonResult jsonResult;
        while (true) {
            doc = webClientUtil.getDoc(link + page);
            if (doc == null) {
                return error("获取该公众号文章列表链接获取失败");
            }
            elements = doc.select("div.mp-article-title");
            if (elements == null || elements.size() == 0) {
                break;
            }
            for (Element element : elements) {
                if (type == 1) {
                    if (element.select("span.like-original") == null || element.select("span.like-original").size() == 0) {
                        continue;
                    }
                }
                src = element.select("a").attr("href");
                if (StringUtils.isNotBlank(src)) {
                    jsonResult = crawlWeChatLink(src, monitorWechat);
                    if (jsonResult.getStatus() == JsonResult.STATUS_ERROR) {
                        ero++;
                        if (ero >= 10) {
                            return error("连续10次导入失败，核实原因");
                        }
                    }
                    if (jsonResult.getStatus() == JsonResult.STATUS_SUCCESS) {
                        success++;
                    }
                    ero = 0;
                }
            }
            page++;
            DateUtil.sleep(10);
        }
        return error("全部导入完毕,累计导入:" + success + " 导入失败：" + ero);
    }


    /**
     * @param id       监测用户的记录Id
     * @param weChatId 微信公众号的Id
     * @return 添加结果
     */
    public JsonResult crawlByCSM(Integer id, String weChatId, Integer num, boolean isCopy) throws InterruptedException {
        //http://chuansong.me/account/laidianer2016?start=12
        //http://chuansong.me/account/yukhun521
        int page = 0;
        MonitorWechat monitorWechat;
        Elements elements;
        JsonResult jsonResult;
        List<String> linkList = new ArrayList<String>();
        String link = "http://chuansong.me/account/" + weChatId;
        Document doc = webClientUtil.getDoc(link);
        if (doc == null) {
            return error("解析CSM失败，请联系技术大大们");
        }
        Thread.sleep(1000);
        if (id == null) {
            monitorWechat = getM(weChatId, "");
        } else {
            monitorWechat = articleMonitorMapper.queryById(id);
        }
        try {
            while (page <= CrawlConstant.CRAWL_PAGE_TOTAL_CHUANSONGMEN) {
                if (doc == null) {
                    return error("获取文章列表失败，link=" + link + "?start=" + page);
                }
                elements = doc.select("a.question_link");
                if (elements == null || elements.size() == 0) {
                    break;
                }
                for (Element element : elements) {
                    linkList.add(element.attr("href"));
                }
                jsonResult = insert(linkList, monitorWechat, num, isCopy);
                if ((Integer) jsonResult.getData() < 1) {
                    logger.info("文章导入结束，可能所有文章更新完毕也可能导入出现错误，请核实 链接：" + link);
                }
                linkList.clear();
                doc = webClientUtil.getDoc(link + "?start=" + (page += 12));
                if (doc == null) {
                    RedisUtils.changeIP(15);
                    doc = webClientUtil.getDoc(link + "?start=" + page);
                }

                DateUtil.sleep(10);
            }


        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return success("更新结束");
    }


    @SuppressWarnings("unchecked")
    public JsonResult crawl(String link, Integer weChatId, Integer num, Boolean isCopy) throws Exception {
        if (link.contains("Windows")) {
            link = link.substring(0, link.indexOf("Windows"));
        }
        String cookie = getCookie(link);
        if (StringUtils.isBlank(cookie)) {
            return error("未获取到Cookie");
        }
        MonitorWechat monitorWechat = null;
        Map<String, String> cookMap = new HashMap<String, String>();
        cookMap.put("Cookie", cookie);
        if (weChatId != null) {
            monitorWechat = articleMonitorMapper.queryById(weChatId);
        }
        Long id = 4294967295l;
        List<String> stringList = new ArrayList<String>();
        String uin = link.substring(link.indexOf("uin="), link.indexOf("&key"));
        String biz = link.substring(link.indexOf("biz="), link.indexOf("&uin"));
        String key = link.substring(link.indexOf("key="), link.indexOf("&devicetype"));
        if (StringUtils.isBlank(uin)) {
            return error("链接为空，请核对连接");
        }
        uin = WebUtil.URLDecode(uin);
        //直接获取链接的uin 和biz按照下列参数书讯写上，再获取Cookie，就不怕微信改版了，（参数和Cookie可以根据单片链接获取还没测试）
        //https://mp.weixin.qq.com/mp/getmasssendmsg?__biz=Mjk2NzMwNTY2MA==&f=json&frommsgid=1000000451&count=10
//mp.weixin.qq.com/mp/getmasssendmsg?__biz=MzA3NTg3MTAyOQ==&uin=&f=json&frommsgid=1000000183&count=10
        //http://mp.weixin.qq.com/mp/getmasssendmsg?__biz=MzI3NjQ2MDUyNA==&uin=MTkxMzQ4NTYyMQ==&key=9ed31d4918c154c8f39511645fa6f2efd6d724f816e660e68e45a9250141a7629d6beca739fb1587a6da0cc55e21c512a9e1a1f13734f188fbf45420c06608263e91084646e592e865e5484f5b4ed16d&f=json&frommsgid=1000000039&count=10&uin=MTkxMzQ4NTYyMQ%3D%3D&key=9ed31d4918c154c8f39511645fa6f2efd6d724f816e660e68e45a9250141a7629d6beca739fb1587a6da0cc55e21c512a9e1a1f13734f188fbf45420c06608263e91084646e592e865e5484f5b4ed16d&pass_ticket=VxtVmLj3RkqHwX6NQpfJPMpHIK1PLQNQKaw1yRlU%25252FBzCYAzm8KlfuCo8cIm4mx33&wxtoken=&x5=0
        link = "http://mp.weixin.qq.com/mp/getmasssendmsg?__" + biz + "&" + uin + "&" + key + "&f=json&frommsgid=" + id + "&count=10&" + uin + "&" + key + "&pass_ticket=Z3S4eBdDDVNYk5%25252B9KWWaRKzGh265taeg%25252B0SB4DnK6xLEouOX0uXmKFd0104T7F6E&wxtoken=&x5=0";
        logger.info("获取数据的链接：" + link);
        int errorSum = 0;
        String json;
        Map<String, Object> map1;
        Map<String, Object> map2;
        List<Map<String, Object>> mapList;
        Map<String, Object> mapWId;
        Map<String, Object> mapWTitle;
        List<Map<String, Object>> mapUrlList;
        while (id > 0) {
            errorSum++;
            logger.info("每次获取数据时休眠5秒");
            DateUtil.sleep(5);
            json = WebUtil.buildRequestData(link, true, cookMap);
            if (StringUtils.isBlank(json)) {
                break;
            }
            //第一层
           map1 = (Map) JsonUtil.getResult(json);
            if (map1 == null || map1.size() == 0) {
                if (errorSum < 5) {
                    Thread.sleep(2000);
                    continue;
                } else {
                    break;
                }
            }
            //第二层
            json = (String) map1.get("general_msg_list");
            map2 = (Map) JsonUtil.getResult(json);
            if (map2 == null || map2.size() == 0) {
                if (errorSum < 5) {
                    Thread.sleep(2000);
                    continue;
                } else {
                    break;
                }
            }
            mapList = (List) map2.get("list");
            if (ListUtil.isEmpty(mapList) || mapList.size() == 0) {
                break;
            }
            errorSum = 0;
            for (int i = 0; i < mapList.size(); i++) {
                if (i == mapList.size() - 1) {
                    mapWId = (Map) mapList.get(i).get("comm_msg_info");
                    if (mapWId == null || mapWId.size() == 0) {
                        return error("设置Id时出现问题，请修复");
                    }
                    id = Long.valueOf((Integer) mapWId.get("id"));
                }
                mapWTitle = (Map) mapList.get(i).get("app_msg_ext_info");
                if (mapWTitle == null || mapWTitle.size() == 0) {
                    continue;
                }
                //根据map判断是否需要根据原创标识，再添加链接
                stringList = addUrlList(mapWTitle, stringList, isCopy);
                //   articleCrawlerController.crawlMonitor(userId, url);
                //文章内容里面，有个字段，可能包含多个小文章，判断是否有多个小文章
                mapUrlList = (List) mapWTitle.get("multi_app_msg_item_list");
                if (ListUtil.isEmpty(mapUrlList) || mapUrlList.size() == 0) {
                    continue;
                }
                for (Map<String, Object> map : mapUrlList) {
                    stringList = addUrlList(map, stringList, isCopy);
                }
            }
            link = "http://mp.weixin.qq.com/mp/getmasssendmsg?__" + biz + "&" + uin + "&" + key + "&f=json&frommsgid=" + id + "&count=10&" + uin + "&" + key + "&pass_ticket=Z3S4eBdDDVNYk5%25252B9KWWaRKzGh265taeg%25252B0SB4DnK6xLEouOX0uXmKFd0104T7F6E&wxtoken=&x5=0";
        }

        return insert(stringList, monitorWechat, num, false);
    }

    /**
     * @param map        文章内容的map集合
     * @param stringList 文章链接集合
     * @param isCopy     是否需要根据原创标识判断
     * @return 文章链接集合
     */
    public List<String> addUrlList(Map<String, Object> map, List<String> stringList, Boolean isCopy) {
        String url;
        if (isCopy) {
            //整理出来的规则，这个字段有11或是12，说明是原创
            Integer copyright_stat = (Integer) map.get("copyright_stat");
            if (copyright_stat == null || (copyright_stat == 100 || copyright_stat == 101)) {
                return stringList;
            }
        }
        url = (String) map.get("content_url");
        if (StringUtils.isBlank(url)) {
            return stringList;
        }
        url = url.replace("\\", "").replace("amp;", "");
        stringList.add(url);
        /*if (weChatId == null && monitorWechat == null) {
                monitorWechat = getMonitorWeChat(url);
            }*/
        return stringList;
    }

    public String getCookie(String link) {
        URL url;
        String cookie = "";
        try {
            //  link = "https://mp.weixin.qq.com/mp/getmasssendmsg?__biz=MjM5ODY1NDMwMA==&uin=MTkxMzQ4NTYyMQ%3D%3D&key=e046222298c2933686296da790e331a24837e3a10e9bd74bdd709e63b071ca93e2a00db638671a39850db2d024fa8682c9ee3f0d47a8f5f51c7c2c1484ba880a10c1402af029cd86defb38d42fd9f4ba&devicetype=Windows+7&version=6203005d&lang=zh_CN&ascene=7&pass_ticket=LVCAKNBq7mO6SeDfmOwUURUV%2B2L6dt4Vxq0M4xNCDcW2Chp79m8d%2FpGe2KliJJZo#wechat_webview_type=1&winzoom=1";
            url = new URL(link);
            URLConnection conn = url.openConnection();
            cookie = conn.getHeaderField("Set-Cookie");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return cookie;
    }

   /* public static void main(String[] args) {
        //  trade("",94,"",0);

    }*/


    @SuppressWarnings("unchecked")
    public MonitorWechat getMonitorWeChat(String url) throws InterruptedException {
        DateUtil.sleep(5);
        MonitorWechat monitorWechat = null;
        String weChatName = "";
        Document doc = getDoc(url);
        Element nameElement = doc.select("span.profile_meta_value").first();
        if (nameElement == null) {
            return null;
        }
        String weChatNo = nameElement.text();
        Element weCharName = doc.select("strong.profile_nickname").first();
        if (weCharName != null) {
            weChatName = weCharName.text();
        }
        //如果有微信号名称，则根据微信号名称找到微信号No(唯一)
        if (StringUtils.isNotBlank(weChatNo)) {
            if (StringUtils.isNotBlank(weChatNo)) {
                monitorWechat = getM(weChatNo, weChatName);
            }
        }
        return monitorWechat;
    }

    public MonitorWechat getM(String weChatNo, String wechatName) {
        MonitorWechat monitorWechat = articleMonitorMapper.queryByWeChatNo(weChatNo);
        if (monitorWechat == null) {
            monitorWechat = new MonitorWechat();
            monitorWechat.setWechatNo(weChatNo);
            monitorWechat.setWechatName(wechatName);
            monitorWechat.setUserId(0);
            articleMonitorMapper.insertWechat(monitorWechat);
        }
        return monitorWechat;
    }


    public JsonResult crawlWeChatLink(String link, MonitorWechat monitorWechat) throws Exception {
        int tag = CrawlerUtil.tag(link);
        if (tag == -1) {
            logger.error("该链接导入失败：链接有误" + link);
            return error("该链接导入失败：链接有误");
        }
        BaseParser baseParser = articleCrawlerService.parserMap.get(tag);
        if (baseParser == null) {
            logger.error("该链接导入失败：链接有误" + link);
            return error("该链接导入失败：链接有误");
        }

        JsonResult jsonResult = articleCrawlerController.weChatCrawler(monitorWechat, link, baseParser);
        if (jsonResult.getStatus() == JsonResult.STATUS_ERROR) {
            RedisUtils.changeIP(15);
        } else {
            return jsonResult;
        }
        return articleCrawlerController.weChatCrawler(monitorWechat, link, baseParser);
    }

    public Document getDocument(String url) {
        Document document = webClientUtil.getDoc(url);
        if (document == null) {
            redisUtils.changeIP(15);

        }
        return document = webClientUtil.getDoc(url);
    }

    public JsonResult insert(List<String> stringList, MonitorWechat monitorWechat, Integer num, boolean isCopy) throws InterruptedException {
        JsonResult jsonResult;
        String url;
        Document document;
        int sum = 0;
        //如果数量不为0，则将超出数量部分文章删除
        if (num != 0) {
            stringList = getList(stringList, num);
        }
        int err = 0;
        for (int i = stringList.size(); i > 0; i--) {
            if (err >= 10) {
                logger.info("连续出现10条已导入数据，终止导入");
                break;
            }
            try {
                url = "http://chuansong.me" + stringList.get(i - 1);
                if (isCopy) {
                    document = getDocument(url);
                    if (document == null) {
                        continue;
                    }
                    if (document.select("span#copyright_logo").first() == null) {
                        continue;
                    }
                }
                jsonResult = crawlWeChatLink(url, monitorWechat);
                if (jsonResult.getStatus() == JsonResult.STATUS_ERROR) {
                    err++;
                    continue;
                }
                if (jsonResult.getStatus() == JsonResult.STATUS_SUCCESS) {
                    err = 0;
                    sum++;
                }
            } catch (Exception e) {
                e.printStackTrace();
                logger.info(e.getMessage());
            }
        }
        return success(sum);
    }

    /**
     * @param stringList 链接列表
     * @param num        数量
     * @return 保留前X篇文章
     */
    public List<String> getList(List<String> stringList, Integer num) {
        int i = 0;
        List<String> srcList = new ArrayList<String>();
        for (String link : stringList) {
            i++;
            if (i > num) {
                srcList.add(link);
            }
        }
        stringList.removeAll(srcList);
        return stringList;
    }


    public Document getDoc(String link) {
        HtmlPage htmlPage = webClientUtil.getPageJs(link);
        if (htmlPage == null) {
            return null;
        }
        String html = htmlPage.asXml();
        Document doc = Jsoup.parse(html);
        return doc;
    }

    /*public JsonResult getBaiDuPirateList(){
        ArticleMonitorMapper
    }*/

    public JsonResult checkSource(String signatureList, String src) {
        HtmlPage htmlPage = webClientUtil.getPage(src);
        Document doc = Jsoup.parse(htmlPage.asXml());
        if (doc == null) {
            return error("没有获取到信息");
        }
        //如果标题获取不到，说明改版了
        Element titleElement = doc.select("h2#activity-name").first();
        if (titleElement == null) {
            return error("没有获取到信息");
        }
        //如果能获取标题却获取不到出处，说明该文没有出处
        Element element = doc.select("a#copyright_info").first();
        if (element == null) {
            return error(0 + "");
        }
        String content = element.text().replace(" ", "");
        if (StringUtils.isBlank(content)) {
            return error(0 + "");
        }
        String[] arraySignature = signatureList.split(",");

        for (String name : arraySignature) {
            name = name.trim();
            if (content.contains(name) && content.contains("内容转载自公众号")) {
                return success(1);
            }
        }
        return success(0);
    }

    public JsonResult checkName(String signatureList, String src) {
        if (StringUtils.isBlank(signatureList)) {
            return error("没有署名");
        }
        String[] arraySignature = signatureList.split(",");
        HtmlPage htmlPage = webClientUtil.getPage(src);
        Document doc = Jsoup.parse(htmlPage.asXml());
        if (doc == null) {
            return error("打开连接失败");
        }
        //对头部署名进行审核
        if (headCheck(doc, arraySignature)) {
            return success(1);
        }
        //对内容署名进行审核
        if (bodyCheck(doc, arraySignature)) {
            return success(1);
        }

        return success(0);
    }

    //微信署名头部信息
    public Boolean headCheck(Document doc, String[] arraySignature) {
        Elements element = doc.select("span.rich_media_meta_tex");
        if (element == null || element.size() < 2) {
            return false;
        }
        String signature = element.get(1).text();
        if (StringUtils.isBlank(signature)) {
            return false;
        }
        //判断显示署名的位置有没有
        for (String name : arraySignature) {
            //去除首位空格，防止添加时含有空格
            name = name.trim();
            if (name.equals(signature)) {
                return true;
            }
        }
        return false;
    }

    public Boolean bodyCheck(Document doc, String[] arraySignature) {
        Elements element = doc.select("div#img-content");
        if (element == null) {
            return false;
        }
        String content = element.first().text();
        if (StringUtils.isBlank(content)) {
            return false;
        }
        //去除文章内容的空格
        content = content.replace(" ", "");
        for (String name : arraySignature) {
            //去除首位空格，防止添加时含有空格
            name = name.trim();
            for (String s : getNameList()) {
                if (content.contains(s + name)) {
                    return true;
                }
            }
        }
        return false;
    }

    public List<String> getNameList() {
        List<String> sourceList = new ArrayList<String>();
        sourceList.add("|");
        sourceList.add("/");
        sourceList.add("：");
        sourceList.add(":");
        sourceList.add("文");
        return sourceList;
    }

    /**
     * @param token    微信公众号登陆的标识  token
     * @param wechatId 微信公众号标识 MTMwNDMwODQ0MQ==
     * @param cookie   登陆后的cookie
     * @return
     */
    public JsonResult crawlPublicWechat(String token, String wechatId, String cookie, HttpServletRequest request, HttpServletResponse response) throws InterruptedException, IOException {

        // jedisUtil.set(wechatId+"import_type","type="+type+",titleKey="+titleKey+",contentKey="+contentKey);
        int page = (Integer)redisUtils.get(wechatId + "_page", 1);
        int articleNum = 0;//文章实际总数
        int allNum = 0; //文章解析总数
        int success = 0; //成功导入书数
        int ero = 0; // 失败数
        int warm = 0; // 警告数
        String eroLink = ""; // 错误链接
        String warmLink = ""; //警告链接
        String link;
        Random random = new Random();
        List<String> links = new ArrayList<String>();
        //https://mp.weixin.qq.com/cgi-bin/searchbiz?action=search_biz&token=69272936&lang=zh_CN&f=json&ajax=1&query=geekpark&begin=0&count=5
        Map<String, String> cookieMap = new HashMap<String, String>();
        cookieMap.put("cookie", cookie);
        String json = WebUtil.buildRequestData("https://mp.weixin.qq.com/cgi-bin/searchbiz?action=search_biz&token=" + token + "&lang=zh_CN&f=json&ajax=1&query=" + WebUtil.URLEncode(wechatId) + "&begin=0&count=5", false, cookieMap);
        if (StringUtils.isBlank(json)) {
            return error("获取微信号唯一标识出错，publicWechatId = " + wechatId);
        }
        Map<String, Object> map = JsonUtil.getResult(json);
        List<Map<String, String>> mapList = (List) map.get("list");
        if (ListUtil.isEmpty(mapList)) {
            return error("搜索不到该公众号的信息,或cookie已过期");
        }
        String fakeid = "";
        for (Map<String, String> map1 : mapList) {
            if (map1.get("alias").trim().equals(wechatId.trim())) {
                fakeid = map1.get("fakeid");
            }
        }
        if (StringUtils.isBlank(fakeid)) {
            return error("获取微信号唯一标识出错，fakeid = " + fakeid);
        }
        //https://mp.weixin.qq.com/cgi-bin/appmsg?token=797603277&f=json&action=list_ex&begin=0&fakeid=MTMwNDMwODQ0MQ%3D%3D&type=9

        //微信公众号封的是平台账号，换代理没用
        int num = 0;
        while (true) {
            redisUtils.set(wechatId + "_page", page + "");
            //每打开十页改变一次IP
            if (num++ % 10 == 0) {
                redisUtils.changeIP(1);
            }
            DateUtil.sleep(70);
            //请求新数据时，重新实例化wc尽量减少一个IP一直请求
            WebClientUtil.wc = null;
            json = WebUtil.buildRequestData("https://mp.weixin.qq.com/cgi-bin/appmsg?token=" + token + "&f=json&action=list_ex&begin=" + (page - 1) * 5 + "&fakeid=" + WebUtil.URLEncode(fakeid) + "&type=9", false, cookieMap);
            // DateUtil.sleep(90);
            //https://mp.weixin.qq.com/cgi-bin/appmsg?token=116120257&lang=zh_CN&f=json&ajax=1&random=0.30035918889479785&action=list_ex&begin=10&count=5&query=&fakeid=MjM5NDIyMzg1Mg%3D%3D&type=9
            System.out.println("https://mp.weixin.qq.com/cgi-bin/appmsg?token=" + token + "&f=json&action=list_ex&begin=" + (page - 1) * 5 + "&fakeid=" + fakeid + "&type=9");
            map = JsonUtil.getResult(json);
            mapList = (List) map.get("app_msg_list");
            if (ListUtil.isEmpty(mapList)) {
                break;
            }
            if (page == 1) {
                articleNum = (Integer) map.get("app_msg_cnt");
            }
            page++;
            for (Map<String, String> map1 : mapList) {
                allNum++;
                link = map1.get("link");
                links.add(link);
            }
        }
        HSSFWorkbook wb = export(links);
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition", "attachment;filename=student.xls");
        OutputStream ouputStream = response.getOutputStream();
        wb.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();

        redisUtils.set(wechatId + "_page", 1);
        logger.info("文章实际总数：" + articleNum + "文章总数：" + allNum + ", 成功导入：" + success + ", 失败数：" + ero + ", 警告数：" + warm + "，失败链接：" + eroLink + ",警告链接：" + warmLink);
        return success("文章实际总数：" + articleNum + "文章总数：" + allNum + ", 成功导入：" + success + ", 失败数：" + ero + ", 警告数：" + warm + "，失败链接：" + eroLink + ",警告链接：" + warmLink);
    }

    public boolean getResult(String style, String key, Document document) {
        if (StringUtils.isNotBlank(key)) {
            String content = document.select(style).text();
            return (StringUtils.isNotBlank(content) && content.contains(key));
        }
        return false;
    }


    public HSSFWorkbook export(List<String> list) {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("微信链接列表");
        HSSFRow row = sheet.createRow(0);
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);


        HSSFCell cell = row.createCell(0);
        cell.setCellValue("微信链接");
        cell.setCellStyle(style);
        sheet.autoSizeColumn(0);
        // sheet.SetColumnWidth(i, 100 * 256);


        for (int i = 0; i < list.size(); i++) {
            row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(list.get(i));
        }
        //row = sheet.createRow(0);
        //row.createCell(0).setCellValue("type=" + type + ",titleKey=" + titleKey + ",contentKey=" + contentKey);
        return wb;
    }

    public JsonResult addWechat(List<String> links, int userId, int type, String titleKey, String contentKey) {
        int articleNum = 0;//文章实际总数
        int allNum = 0; //文章解析总数
        int success = 0; //成功导入数
        int ero = 0; // 失败数
        int warm = 0; // 警告数
        Document document;
        JsonResult jsonResult;//保存结果
        try {
            for (String link : links) {
                WebClientUtil.wc = webClientUtil.initWc(false, false, true);
                if (StringUtils.isBlank(link)) {
                    continue;
                }
                document = weChatCheckService.getDoc(link);
                if (document == null) {
                    return error("IP连续更换三次还是被封");
                }
                //如果需要导入原创且原创标识没有、标题和内容都不含关键词则跳过
                if (type == 1) {
                    String content = document.select("span#copyright_logo").text();
                    if (StringUtils.isBlank(content)) {
                        continue;
                    }
                }
                jsonResult = articleCrawlerController.crawlMonitor(userId, link);
                if (jsonResult.getStatus() == JsonResult.STATUS_SUCCESS) {
                    success++;
                }
                if (jsonResult.getStatus() == JsonResult.STATUS_ERROR) {
                    logger.error("出错链接：" + link);
                    ero++;
                }
                if (jsonResult.getStatus() == JsonResult.STATUS_WARN) {
                    logger.error("警告链接：" + link);
                    warm++;
                }

            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        logger.info("文章实际总数：" + articleNum + "文章总数：" + allNum + ", 成功导入：" + success + ", 失败数：" + ero + ", 警告数：" + warm);
        return success("文章实际总数：" + articleNum + "文章总数：" + allNum + ", 成功导入：" + success + ", 失败数：" + ero + ", 警告数：" + warm);

    }

}
